// Include system file
#include "r_cg_can.h"

// Include project file
#include "CAN.h"
#include "fet.h"
#include "nvm.h"
#include "control.h"
#include "faults.h"
#include "Main.h"
#include "Switches.h"
#include "Joystick.h"
#include "Serial.h"
#include "Battery.h"
#include "RemoteCtrlCan.h"

structRemoteCtrlCan stRemoteCtrlCan={0};
static void vProcRemoteCtrlLogic(void);
static UINT8 u8ProcWalkGear(UINT8 Byte);



void vRemoteCtrlCanFastRxCallBack(UINT16 u16_id_hi,UINT16 u16_id_lo,UINT8 u8_length,UINT8 *pData)
{
	UINT8 i=0;
	if(u16_id_hi==0&&u16_id_lo==0x181&&u8_length==8)
	{
		for(i=0;i<8;i++)
		{
			stRemoteCtrlCan.unRx0x181.Bytes[i] = pData[i];
		}
		stRemoteCtrlCan.stRxCtrl.fRx0x181Update=1;
	}
}

static void vProcRemoteCtrlCanLost(void)//内部调用，掉线逻辑
{
	UINT8 i=0;
	if(stRemoteCtrlCan.stRxCtrl.fRx0x181Update)
	{
		stRemoteCtrlCan.stRxCtrl.u16Rx0x181LostCnt=0;
		stRemoteCtrlCan.stRxCtrl.fRx0x181Lost=0;
	}
	if(stRemoteCtrlCan.stRxCtrl.u16Rx0x181LostCnt<RemoteCtrlCanLostTime)
	{
		stRemoteCtrlCan.stRxCtrl.u16Rx0x181LostCnt++;
	}
	else
	{
		if(stRemoteCtrlCan.stRxCtrl.fRx0x181Lost!=1)
		{
			stRemoteCtrlCan.stRxCtrl.fRx0x181Lost=1;	
			for(i=0;i<8;i++)
			{
				stRemoteCtrlCan.unRx0x181.Bytes[i] = 0;
			}
			stRemoteCtrlCan.stOutPut.s8OutValue=0;
		}
	}
}
void vRemoteCtrlCanRx(void)//10ms 主循环
{
	vProcRemoteCtrlCanLost();
	if(stRemoteCtrlCan.stRxCtrl.fRx0x181Update)
	{
		stRemoteCtrlCan.stRxCtrl.fRx0x181Update = 0;
		
		//数据更新,处理数据
		vProcRemoteCtrlLogic();
		stRemoteCtrlCan.stOutPut.u8Gear=u8ProcWalkGear(stRemoteCtrlCan.unRx0x181.Bytes[2]);
	}
}
void vRemoteCtrlCanTx(void)//10ms 主循环
{
	static UINT8 u830msCnt=0;	
	if(++u830msCnt>=3){u830msCnt=0;}
	switch(u830msCnt)
	{
		case 1:
			if(TX_FIFO_PUSH_EN)
			{
				vPushCanTxFifo(&stCanTxFifo,0x0000, 0x0201, 8, &stRemoteCtrlCan.unTx0x201.Bytes[0]);
			}
		break;
		default:
		break;
	}
}
//
static UINT8 u8ProcWalkGear(UINT8 Byte)
{
	UINT8 i;
	for(i=0;i<6;i++)
	{
		if(((Byte>>i)&1)!=0)
		{
			break;
		}
	}
	/*
	if(i>5)//0-5
	{
		i=0;
	}
	*/
	return i;
}

static void vProcRemoteCtrlLogic(void)
{
	INT16 s16Temp;
	/*
	struct
	{
		UINT8 fButtonEnable:1;
		UINT8 fButtonHorn:1;
		UINT8 fButtonReleaseBrake:1;
		UINT8 fButtonLamp:1;
		UINT8 u8Byte0NC:4;

		UINT8 fButtonPlantformL_Out:1;
		UINT8 fButtonPlantformL_In:1;
		UINT8 fButtonPlantformR_Out:1;
		UINT8 fButtonPlantformR_In:1;
		UINT8 fButtonRangeExtenderStartOrStop:1;
		UINT8 fButtonRangeExtenderPreHeat:1;
		UINT8 u8Byte1NC:2;

		UINT8 fKnob_1:1;
		UINT8 fKnob_2:1;
		UINT8 fKnob_3:1;
		UINT8 fKnob_4:1;
		UINT8 fKnob_5:1;
		UINT8 fKnob_6:1;
		UINT8 u8Byte2NC:2;

		UINT8 u8Byte3NC;
		UINT8 u8Byte4NC;

		UINT8 u8SpeedRef;//0-255

		UINT8 u8Joystick;//后退：0-127,中位：128,前进：129-255
		UINT8 u8Rocker;//左转：0-127,中位：128,右转：129-255
	}bit;
	*/
	if(stRemoteCtrlCan.unRx0x181.bit.fButtonEnable)
	{
		s16Temp = (INT16)stRemoteCtrlCan.unRx0x181.bit.u8Joystick-128;//-128~+127
		stRemoteCtrlCan.stOutPut.s8OutValue=s16Temp*100/127;
		if(stRemoteCtrlCan.stOutPut.s8OutValue>100)stRemoteCtrlCan.stOutPut.s8OutValue=100;
		if(stRemoteCtrlCan.stOutPut.s8OutValue<-100)stRemoteCtrlCan.stOutPut.s8OutValue=-100;

		s16Temp = (INT16)stRemoteCtrlCan.unRx0x181.bit.u8Rocker-128;//-128~+127
		stRemoteCtrlCan.stOutPut.s8OutValueRocker=s16Temp*100/127;
		if(stRemoteCtrlCan.stOutPut.s8OutValueRocker>100)stRemoteCtrlCan.stOutPut.s8OutValueRocker=100;
		if(stRemoteCtrlCan.stOutPut.s8OutValueRocker<-100)stRemoteCtrlCan.stOutPut.s8OutValueRocker=-100;
	}
	else
	{
		stRemoteCtrlCan.stOutPut.s8OutValue=0;
	}
}


